//
//  ZYBIgCollectionCell.m
//  Project_B
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 ZY. All rights reserved.
//

#import "ZYBIgCollectionCell.h"
#import "ZYPopularViewCell.h"
#import "ZYBigImgViewCell.h"
#import "ZYImageViewCell.h"
#import "ZYHeaderImgCell.h"

@interface ZYBIgCollectionCell ()<UITableViewDataSource,UITableViewDelegate,ZYHeaderImgDeleage>

@property(nonatomic,strong)UITableView *myTableView;/**< 最新的TableView*/

@property(nonatomic,copy)NSString *url;
@property(nonatomic,copy)NSString *footerUrl;

@property(nonatomic,strong)NSMutableArray *dataArray;

@property(nonatomic,assign)NSInteger beginPage;
@property(nonatomic, assign)BOOL isUpLoad;

@end

@implementation ZYBIgCollectionCell

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self cresteSubView];
    }
    return self;
}



-(void)cresteSubView{
    
    
    [self makeTableView];
    
    self.dataArray = [NSMutableArray array];
    self.beginPage = 0;
}


#pragma mark - tableView
-(void)makeTableView{
    
    
    self.myTableView = [[UITableView alloc]init];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.contentView addSubview:self.myTableView];
    self.myTableView.rowHeight = (HEADERIMG_HEIGHT + SPACE*2 + BIGIMG_HEIGHT + SPACE*7 + IMG_HEIGHT +SPACE*4 + SCREEN_HEIGHT/3.5)/4;
    
    
    [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(self.contentView.frame.size.height);
        
    }];
    
    //注册 PopularCell
    [self.myTableView registerClass:[ZYPopularViewCell class] forCellReuseIdentifier:@"ZYPopularViewCell"];
    //注册 imgCell
    [self.myTableView registerClass:[ZYImageViewCell class] forCellReuseIdentifier:@"ZYImageViewCell"];
    //注册 bigImgCell
    [self.myTableView registerClass:[ZYBigImgViewCell class] forCellReuseIdentifier:@"ZYBigImgViewCell"];
    //注册 headerImgCell
    [self.myTableView registerClass:[ZYHeaderImgCell class] forCellReuseIdentifier:@"ZYHeaderImgCell"];
}





#pragma mark - 设置行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}





#pragma mark - 设置cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    

    
    NewestModel *model = [self.dataArray objectAtIndex:indexPath.row];
    
    if (indexPath.row == 0) {
        NewestModel *topModel = [self.dataArray firstObject];
        
        ZYHeaderImgCell *headerCell = [tableView dequeueReusableCellWithIdentifier:@"ZYHeaderImgCell"];
        headerCell.model = topModel;
        
        //设置代理人
        headerCell.headerImgDeleage = self;
        
        return headerCell;
    }else{
    
     if (model.imgextra != nil && model.ads == nil) {
        
         ZYImageViewCell *imgCell = [tableView dequeueReusableCellWithIdentifier:@"ZYImageViewCell"];
        imgCell.model = model;
        
        return imgCell;
    }else if(1 == [model.imgType integerValue]){
        
        ZYBigImgViewCell *bigImgCell = [tableView dequeueReusableCellWithIdentifier:@"ZYBigImgViewCell"];
        bigImgCell.model = model;
        
        return bigImgCell;
    }else{
        
            ZYPopularViewCell *popularCell = [tableView dequeueReusableCellWithIdentifier:@"ZYPopularViewCell"];
            popularCell.model = model;
            
       return popularCell;
    }
   
}
    
}






#pragma mark - 设置cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewestModel *model = [self.dataArray objectAtIndex:indexPath.row];

    if (indexPath.row == 0) {
        
        return SCREEN_HEIGHT/3.5;
    }else {
        if (model.imgextra != nil && model.ads == nil) {
        
            return IMG_HEIGHT +SPACE*4;
        }else if(1 == [model.imgType integerValue]){
            
            return BIGIMG_HEIGHT + SPACE*7;
        }else{
            
            return HEADERIMG_HEIGHT + SPACE*2;
        }
    
    }
}


#pragma mark - 点击cell时触发
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //取出触摸后 置灰效果
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
    NewestModel *model = [self.dataArray objectAtIndex:indexPath.row];

    [self.bigCollectionDeleage presentDataToController:model];
    
}

#pragma mark - 轮播图协议

-(void)presentShowsDataToController:(NSString *)url :(NSNumber *)replyCount{
    [self.bigCollectionDeleage presentUrlToController:url :replyCount ];
}




#pragma mark - 拼接网址
-(void)setUrlTidStr:(NSString *)urlTidStr{
    _urlTidStr = urlTidStr;
    
    if ([_urlTidStr isEqualToString:@"T1348647853363"]) {
        self.url = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/headline/%@/0-20.html",_urlTidStr];
    }else{
        self.url = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/%@/0-20.html",_urlTidStr];
    }
    
    
    
    
    [self getData];
    [self addHeaderRefresh];
    [self addFooterRefresh];
//    NSLog(@"s = %@",_url);
    
    

}


#pragma mark - 请求数据
-(void)getData{
    

    
    [ZYTools getUrl:self.url body:nil result:ZYJSON headerFile:nil success:^(id result) {
        
        if (self.isUpLoad == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.dataArray removeAllObjects];
        }
        
        NSMutableArray *array = [result objectForKey:_urlTidStr];
        for (NSMutableDictionary *dic in array) {
            NewestModel *model = [[NewestModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        
        [self.myTableView.mj_header endRefreshing];
        [_myTableView reloadData];
       

        
    } failure:^(NSError *error) {
        
        
    }];
}




#pragma mark - 下拉刷新
-(void)addHeaderRefresh{
    
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        NSLog(@"下拉");
        self.isUpLoad = NO;//标记为下拉操作
        
        [self getData];
    }];
        
        

    
    [self.myTableView.mj_header beginRefreshing];
}

#pragma mark - 上拉刷新
-(void)addFooterRefresh{
    
    
    
    self.myTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        NSLog(@"上拉");
        
        
        self.beginPage = self.beginPage + 20;
        self.isUpLoad = YES;//标记为上拉操作
        
        [self getDataIsFooterRefresh];
        
    }];
    
    
}


#pragma mark - 上拉数据请求
-(void)getDataIsFooterRefresh{
    

    
    if ([_urlTidStr isEqualToString:@"T1348647853363"]) {
        self.footerUrl = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/headline/%@/%ld-20.html", _urlTidStr, _beginPage ];
    }else{
        self.footerUrl = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/%@/%ld-20.html", _urlTidStr, _beginPage];
    }
    



    [ZYTools getUrl:self.footerUrl body:nil result:ZYJSON headerFile:nil success:^(id result) {
        
        
        NSMutableArray *array = [result objectForKey:_urlTidStr];
        for (NSMutableDictionary *dic in array) {
            NewestModel *model = [[NewestModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        
        [self.myTableView.mj_footer endRefreshing];
        [_myTableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
}



@end
